package cn.com.duiba.millionaire.center.biz.dao.impl;

import cn.com.duiba.millionaire.center.biz.constants.DatabaseSchema;
import cn.com.duiba.millionaire.center.biz.dao.BaseDao;
import cn.com.duiba.millionaire.center.biz.dao.CreditsConsumeLogDao;
import cn.com.duiba.millionaire.center.biz.entity.CreditsConsumeLogEntity;
import cn.com.duiba.thirdparty.enums.CreditsConsumeTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoxuda on 2018/2/11.
 */
@Repository
public class CreditsConsumeLogDaoImpl extends BaseDao implements CreditsConsumeLogDao {
    @Override
    public long insert(CreditsConsumeLogEntity entity) {
        super.insert("insert", entity);
        return entity.getId();
    }

    @Override
    public CreditsConsumeLogEntity findById(Long id) {
        return super.selectOne("findById", id);
    }

    @Override
    public boolean updateStatus2Fail(Long id, String devBizId, String error4Admin, String error4Developer, String error4Consumer) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("developerBizId", devBizId);
        param.put("error4Admin", StringUtils.length(error4Admin) > 255 ? error4Admin.substring(0, 255) : error4Admin);
        param.put("error4Developer", StringUtils.length(error4Developer) > 255 ? error4Developer.substring(0, 255) : error4Developer);
        param.put("error4Consumer", StringUtils.length(error4Consumer) > 255 ? error4Consumer.substring(0, 255) : error4Consumer);
        return super.update("updateStatus2Fail", param) == 1;
    }

    @Override
    public boolean updateStatus2Success(Long id, String devBizId) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("developerBizId", devBizId);
        return super.update("updateStatus2Success", param) == 1;
    }

    @Override
    public List<CreditsConsumeLogEntity> findNoFailByConsumerIdAndBizTypeAndBizId(Long consumerId,
                                                                                  CreditsConsumeTypeEnum bizType,
                                                                                  String bizNo) {
        Map<String, Object> param = new HashMap<>();
        param.put("consumerId", consumerId);
        param.put("bizType", bizType.getCode());
        param.put("bizNo", bizNo);
        return super.selectList("findNoFailByConsumerIdAndBizTypeAndBizId", param);
    }

    @Override
    protected DatabaseSchema chooseSchema() {
        return DatabaseSchema.MILLIONAIRE_CONFIG;
    }
}
